﻿/***********************************************************************
 * <copyright file="SqlServerItemTransactionDao.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   BangNC
 * Email:    BangNC@buca.vn
 * Website:
 * Create Date: Friday, March 14, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.StandartInterfaces.Dictionary;
using DataObjects.StandartInterfaces.Voucher;


namespace DataObjects.ADO.SqlServer.Voucher
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerItemTransactionDao : IItemTransactionDao
    {
    

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, ItemTransaction> Make = reader =>
            new ItemTransaction
            {
                RefId = reader["RefID"].AsLong(),
                RefTypeId = reader["RefTypeID"].AsInt(),
                RefNo = reader["RefNo"].AsString(),
                RefDate = reader["RefDate"].AsDateTime(),
                CustomerId = reader["CustomerID"].AsString().AsInt(),
                VendorId = reader["VendorID"].AsString().AsInt(),
                Trader = reader["Trader"].AsString(),
                TotalAmountOc = reader["TotalAmountOc"].AsDecimal(),
                TotalAmountTax = reader["TotalAmountTax"].AsDecimal(),
                StockId = reader["StockID"].AsInt(),
                JournalMemo = reader["JournalMemo"].AsString(),
                DocumentInclude = reader["DocumentInclude"].AsString(),
                TaxCode = reader["TaxCode"].AsString(),
                UnitId = reader["UnitID"].AsLong(),
                AccountingObjectType = reader["AccountingObjectType"].AsInt(),
                IsCalculatePrice = reader["IsCalculatePrice"].AsBool(),
            };

        /// <summary>
        /// Takes the specified itemTransaction.
        /// </summary>
        /// <param name="itemTransaction">The itemTransaction.</param>
        /// <returns></returns>
        private object[] Take(ItemTransaction take)
        {
            return new object[]  
            {
                  "@RefID",take.RefId,
                 "@RefTypeID",take.RefTypeId,
                 "@RefNo",take.RefNo,
                 "@RefDate",take.RefDate,
                 "@CustomerID",take.CustomerId,
                 "@VendorID",take.VendorId,
                 "@Trader",take.Trader,
                 "@StockID",take.StockId,
                 "@TotalAmountOC",take.TotalAmountOc,
                 "@TotalAmountTax",take.TotalAmountTax,
                 "@JournalMemo",take.JournalMemo,
                 "@DocumentInclude",take.DocumentInclude,
                 "@TaxCode",take.TaxCode,
                 "@UnitID" , take.UnitId ,
                  "@AccountingObjectType",take.AccountingObjectType	
            };
        }




        public BusinessObjects.BussinessVoucher.ItemTransaction GetItemTransaction(long itemTransactionId) 
        {
            const string sql = @"uspGet_ItemTransaction_ByID";
            object[] parms = { "@RefID", itemTransactionId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public System.Collections.Generic.List<ItemTransaction> GetPaged(int unitId, int refTypeId, int currentPage)
        {
            const string sql = @"uspGetPaged_ItemTransaction";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId, "@RefTypeID", refTypeId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(int unitId, int refTypeId)
        {
            const string sql = @"uspGetPageSize_ItemTransaction";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId, "@RefTypeID", refTypeId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }
        
        string IItemTransactionDao.InsertItemTransaction(ItemTransaction itemTransaction)
        {
            const string sql = "uspInsert_ItemTransaction";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(itemTransaction));
        }


        public string UpdateItemTransaction(ItemTransaction itemTransaction)
        {
            const string sql = "uspUpdate_ItemTransaction";
            return Db.Update(sql, CommandType.StoredProcedure, Take(itemTransaction));
        }

        public string DeleteItemTransaction(ItemTransaction itemTransaction)
        {
            const string sql = @"uspDelete_ItemTransaction";
            object[] parms = { "@RefID", itemTransaction.RefId,
                             "@UnitID", itemTransaction.UnitId};
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }



        public long GetSequence()
        {
            return Db.GetCount("SELECT NEXT VALUE FOR [dbo].[SEQ_ItemTransaction_ItemTransactionID]", null);
        }
    }
}
